package per.sen.shopping.application.service.impl;

import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import per.sen.shopping.application.service.SysBackendUserService;
import per.sen.shopping.domain.services.SysBackendUserDomain;
import per.sen.shopping.infrastructure.common.response.ApiResponse;
import per.sen.shopping.infrastructure.common.response.HttpStatus;
import per.sen.shopping.model.dto.SysBackendUserDTO;
import per.sen.shopping.model.po.SysBackendUserPO;

@ApplicationScoped
public class SysBackendUserServiceImpl implements SysBackendUserService {
    @Inject
    SysBackendUserDomain sysBackendUserDomain;

    @Override
    @WithTransaction
    public Uni<ApiResponse> login(SysBackendUserDTO sysBackendUserDTO) {
        return sysBackendUserDomain.login(sysBackendUserDTO).onItem().transform(apiResponse -> {
            if(apiResponse.get("code").equals(HttpStatus.SUCCESS.getCode())){
                SysBackendUserPO data = (SysBackendUserPO) apiResponse.get("data");
                //获取令牌
                String token = sysBackendUserDomain.createToken(data);
                apiResponse.put("msg","登录成功");
                apiResponse.put("data",token);
            }
            return apiResponse;
        });

    }
}
